-- startVerifyLogic
-- Create by weism
-- 启动游戏逻辑模块

-- 基础全局数据
require("game/logic/base/Account");
require("game/logic/base/Combat");
require("game/logic/base/Dbase");
require("game/logic/base/Dungeon");
require("game/logic/base/DungeonInstance");
require("game/logic/base/Element");
require("game/logic/base/Equip");
require("game/logic/base/EventConst");
require("game/logic/base/ExpressMapping");
require("game/logic/base/GameAchievement");
require("game/logic/base/Gender");
require("game/logic/base/Item");
require("game/logic/base/MagicStone");
require("game/logic/base/BossStone");
require("game/logic/base/EngineeringManual");
require("game/logic/base/Mine");
require("game/logic/base/Monster");
require("game/logic/base/Prop");
require("game/logic/base/Share");
require("game/logic/base/Skill");
require("game/logic/base/Status");
require("game/logic/base/Style");
require("game/logic/base/Talent");
require("game/logic/base/Task");
require("game/logic/base/Trap");
require("game/logic/base/Arena");
require("game/logic/base/SkyCity");

-- 所有的游戏对象
require("game/logic/clone/Bine");
require("game/logic/clone/Building");
require("game/logic/clone/Equipment");
require("game/logic/clone/GhostShip");
require("game/logic/clone/AvalonCannon");
require("game/logic/clone/Grid");
require("game/logic/clone/Item");
require("game/logic/clone/Monster");
require("game/logic/clone/Pet");
require("game/logic/clone/SmallBuilding");
require("game/logic/clone/Summon");
require("game/logic/clone/User");

require "cocos.cocos2d.extern"
require("game/base/Sequence");

require "core/base/UserDefaultM"

-- loc
require("game/loc/localization");

-- 已加载的模块列表
local loadedModules = {};

-- 有效模块标识列表
_G.validModules = {};

-- 有效的CSV加载函数
_G.validCsvFuncs = {};

-- 正在加载的模块
_G.loadingModule = {};

-- 初始化系统模块
function loadModules()
    -- 初始化已加载模块
    for _, moduleName in pairs(loadedModules) do
        require("game/logic/module/" .. moduleName);

        if _G[moduleName].init then
            _G[moduleName].init();
        end
    end
end

function startLogic()
    math.randomseed(os.time());

    -- 设置_G的元表，在查找不到模块时，尝试加载
    local mt = {
        __index = function(t, key)
            -- 查看是否有效的模块名
            if _G.validModules[key] and not _G.loadingModule[key] then
                -- 避免嵌套使用时出现递归(如代码: t = t or {} )
                _G.loadingModule[key] = true;

                -- 加载模块代码
                require("game/logic/module/" .. key);

                -- 初始化 模块
                if t[key].init then
                    t[key].init();
                end

                -- 加入已加载列表
                table.insert(loadedModules, key);

                return t[key];
            -- 如果是CSV加载函数
            elseif _G.validCsvFuncs[key] then
                -- 加载CSV代码
                require("game/csv/" .. _G.validCsvFuncs[key]);
                return t[key];
            end

            return nil;
        end
    }

    setmetatable(_G, mt);

    -- 初始化CSV
    initCsv();

    -- 初始化系统模块
    initModules();
end

-- 初始化CSV
function initCsv()
    -- 所有的csv列表
    _G.allCsvList = LOAD_RUN("game/csv/list");

    -- 每个CSV对应的get_xxx_csv函数
    for _, csvName in pairs(_G.allCsvList) do
        _G.validCsvFuncs["get_" .. csvName .. "_CSV"] = csvName;
    end
end

-- 只加载必要的模块，其他小模块用到的时候再加载
function initModules()
    -- 需要在初始化时就提前加载的模块
    --（不在这个列表中的模块将在用到的时候才require，并且执行该模块的init函数）
    loadedModules = {
        "DungeonVerifyM", "AttribM", "PropM", "SkillM", "EquipM", "PetM", "ItemM", "SuitM", "FormulaM",
    };

    loadModules();

    -- 所有模块列表
    _G.allGameModules = LOAD_RUN("game/logic/module/list");

    -- 设置有效模块标识
    for _, moduleName in pairs(_G.allGameModules) do
        _G.validModules[moduleName] = true;
    end
end

